package pkg.entity;

import com.google.appengine.api.datastore.Text;
import com.googlecode.objectify.Key;
import java.util.Date;
import javax.persistence.Id;
import pkg.dao.EntityDAO;

public class Auction extends ObjectifyEntity {

    @Id
    Long id;
    Key<Teacher> teacherKey;
    Key<Bid> bidKey;
    Float startAmount;
    Float amount;
    Float increment;
    Date startDate;
    Date endDate;
    String photo;
    byte[] picBlob;
    boolean active;
    String title;
    Text description;

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Float getAmount() {
        return amount;
    }

    public boolean isStarted() {
        Date now = new Date();
        return now.after(startDate);
    }

    public boolean isClosed() {
        Date now = new Date();
        return now.after(endDate);
    }

    public boolean isBiddable() {
        return isActive() && isStarted() && !isClosed();
    }

    public void setAmount(Float amount) {
        this.amount = amount;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTeacherId() {
        return teacherKey.getId();
    }

    public void setTeacherId(Long teacherId) {
        this.teacherKey = new Key<Teacher>(Teacher.class, teacherId);
    }

    public Key<Teacher> getTeacherKey() {
        return teacherKey;
    }

    public void setTeacherKey(Key<Teacher> key) {
        teacherKey = key;
    }

    public Long getBidId() {
        return bidKey != null ? bidKey.getId() : null;
    }

    public void setBidId(Long bidId) {
        this.bidKey = new Key<Bid>(Bid.class, bidId);
    }

    public Key<Bid> getBidKey() {
        return bidKey;
    }

    public void setBidKey(Key<Bid> key) {
        bidKey = key;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Float getIncrement() {
        return increment;
    }

    public void setIncrement(Float increment) {
        this.increment = increment;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public Float getStartAmount() {
        return startAmount;
    }

    public void setStartAmount(Float startAmount) {
        this.startAmount = startAmount;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public String getDescription() {
        if (description != null) {
            return description.getValue();
        } else {
            return null;
        }
    }

    public void setDescription(String description) {
        this.description = new Text(description);

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Teacher getTeacher() {
        Teacher result = null;
        if (teacherKey != null) {
            EntityDAO dao = new EntityDAO();
            result = dao.find(teacherKey);
        }
        return result;
    }

    public Bid getBid() {
        Bid result = null;
        if (bidKey != null) {
            EntityDAO dao = new EntityDAO();
            result = dao.find(bidKey);
        }
        return result;
    }

    public byte[] getPicBlob() {
        return picBlob;
    }

    public void setPicBlob(byte[] picBlob) {
        this.picBlob = picBlob;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Auction other = (Auction) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 67 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public Key<ObjectifyEntity> getPrimaryKey() {
        return new Key(this.getClass(), id);
    }
}
